//
//  FTQRegController.m
//  xiangmu
//
//  Created by apple on 2019/8/14.
//  Copyright © 2019 rzt. All rights reserved.
//

#import "FTQRegController.h"
#import "XieYiViewController.h"


@interface FTQRegController (){
    BOOL select;
}
@property(strong,nonatomic)NSMutableArray* dataArr;
@end

@implementation FTQRegController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArr=[[NSMutableArray alloc] init];
    [self.dataArr addObjectsFromArray:kUserDefaultObjectForKey(@"user")];
}


- (IBAction)backBtnTap:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)selectBtnTap:(id)sender {
    if (!select) {
        [self.selectbtn setBackgroundImage:[UIImage imageNamed:@"协议已勾选"] forState:UIControlStateNormal];
        select=YES;
    }else{
        [self.selectbtn setBackgroundImage:[UIImage imageNamed:@"协议未勾选"] forState:UIControlStateNormal];
        select=NO;
    }
}

- (IBAction)xieyiBtnTap:(id)sender {
    XieYiViewController* xieyi=[[XieYiViewController alloc] init];
    [self.navigationController pushViewController:xieyi animated:YES];
}

- (IBAction)regbtnTap:(id)sender {
    if (self.usernameTextfield.text.length==0) {
        [self.view makeToast:@"请输入账号~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else if (self.passwordTexfield.text.length==0){
        [self.view makeToast:@"请输入密码~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else if (self.againpwdtextfield.text.length==0){
        [self.view makeToast:@"请确认密码~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else if (!select) {
        [self.view makeToast:@"请勾选同意协议~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else if(![self.passwordTexfield.text isEqualToString:self.againpwdtextfield.text]){
        [self.view makeToast:@"两次输入密码不一致~"
                    duration:2.0
                    position:CSToastPositionCenter];
    }else{
        for (int i=0; i<self.dataArr.count; i++) {
            if ([self.usernameTextfield.text  isEqualToString:self.dataArr[i][@"username"]]) {
                [self.view makeToast:@"此用户已存在~"
                            duration:2.0
                            position:CSToastPositionCenter];
                return;
                }
        }
        NSDictionary* dic=[NSDictionary dictionaryWithObjectsAndKeys:self.usernameTextfield.text, @"username", self.passwordTexfield.text, @"password",[self nowTimeSwitchTimestamp],@"date",@"暂无昵称",@"nickname",@"0.00",@"money",@"无",@"zhifupwd",nil];
        [self.dataArr addObject:dic];
        kUserDefaultSetObjectForKey(self.dataArr,@"user");
        MBProgressHUD* HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.completionBlock = ^(){
            [self.navigationController popViewControllerAnimated:YES];
        };
        [HUD hideAnimated:YES afterDelay:1.0];
    }
}
@end
